package com.cn.yunhe.rentcars.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.CarOrderMapper;
import com.cn.yunhe.rentcars.mapper.ClientCompanyMapper;
import com.cn.yunhe.rentcars.mapper.ClientUserMapper;
import com.cn.yunhe.rentcars.mapper.DeptMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.Dept;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 
 * @author 李亚磊
 *	我要租车模块：租车登记
 */

@RestController
public class CarOrdersController {
	/**
	 * 注入CarMapper接口
	 */
	@Autowired
	private CarMapper carMapper;
	
	/**
	 * 注入ClientUserMapper接口
	 */
	@Autowired
	private ClientUserMapper clientUserMapper;
	
	/**
	 * 注入ClientCompanyrMapper接口
	 */
	@Autowired
	private ClientCompanyMapper clientCompanyMapper;
	
	/**
	 * 注入CarOrderMapper接口
	 */
	@Autowired
	private CarOrderMapper carOrderMapper;
	
	/**
	 * 注入日志接口
	 */
	@Autowired
	private BusiLogMapper busiLogMapper;
	
	/**
	 * 注入门店接口
	 */
	@Autowired
	private DeptMapper deptMapper;
	
	/**
	 * 租车登记左侧跳转页面
	 */
	@RequestMapping(value="/registorders", method=RequestMethod.GET)
	public ModelAndView registCarOrders( ){
		return new ModelAndView("/registorders");
	}
	
	/**
	 * 查询汽车状态为6待租赁，显示所有待租赁汽车列表页面
	 */
	@RequestMapping(value="/getcarsa", method=RequestMethod.GET)
	public Map<String,Object> getCars(@RequestParam int draw,@RequestParam int start,@RequestParam int length){
		//TODO:调用接口方法查询数据库
		int pageNum = (start/length)+1;//获取当前查询第几页
		int pageSize = length;
		PageHelper.startPage(pageNum, pageSize);
		List<Car> carList=carMapper.getCarsList("6");
		PageInfo<Car> page=new PageInfo<Car>(carList);
		
		Map<String,Object> rsMap = new HashMap<String,Object>();
		rsMap.put("draw", draw);
		rsMap.put("data", page.getList());	      
	    rsMap.put("recordsTotal", page.getTotal());
	    rsMap.put("recordsFiltered", page.getTotal());
	    return rsMap;
	}
	/**
	 * 查询汽车状态为待租赁6，汽车价格表，汽车表信息
	 * @param 车辆id
	 */
	@RequestMapping(value="/price/getCar", method=RequestMethod.GET)
	public Map<String,Object> getCarPrice(@RequestParam String carid,@RequestParam String getdept,
			@RequestParam String backdept){
		//TODO:根据部门名称查询部门具体地址，查询两次
		String getdeptAddress = deptMapper.getDeptAddress(getdept);
		String backdeptAddress = deptMapper.getDeptAddress(backdept);
		Car car = carMapper.getCarInfo(carid);
		car.setGetdeptAddress(getdeptAddress);
		car.setBackdeptAddress(backdeptAddress);
		car.setTotalprice(car.getDayprice()+car.getBasicinsure());
		Map<String,Object> rsMap = new HashMap<String,Object>();
		rsMap.put("car", car);
		return rsMap;
	}
	/**
	 * 将填写的客户信息插入到客户信息表中t_clientuser
	 * 将填写的客户企业信息插入到客户客户企业信息表中t_cliencompany
	 * @param mobile 	客户电话
	 * @param name   	客户名字
	 * @param idcard 	客户身份证
	 * @param drivernum 客户驾驶证号
	 * @param busilicense 营业执照     图片地址
	 * @param cid  		企业编号
	 * @param orgcode   机构代码
	 * @param taxcertno	税务证号
	 * @param realrent  租赁费用
	 * @param carid  	车辆编号  
	 * @param basicinsure  基本保险
	 * @param btrentdept   取车门店
	 * @param etrentdept	还车门店
	 * @param readybttime	预计取车时间
	 * @param readettime	预计还车时间
	 */
	@RequestMapping(value="/addClient", method=RequestMethod.POST)
	public void addClient(@RequestParam String mobile,@RequestParam String name,@RequestParam String idcard,@RequestParam String drivernum,
			@RequestParam String busilicense,@RequestParam String cid,@RequestParam String orgcode,@RequestParam String taxcertno,
			@RequestParam String shouldrent,@RequestParam String shouldtotal,@RequestParam String carid,@RequestParam String basicinsure,@RequestParam String btrentdept,
			@RequestParam String etrentdept,@RequestParam String readybttime,@RequestParam String readettime,HttpServletRequest request
			){
		//TODO:把客户信息参数封装成map
		Map<String,String> paramsMap=new HashMap<String,String>();
		String uid = UUID.randomUUID().toString();		//设置客户信息主键UUid
		paramsMap.put("uid", uid);
		paramsMap.put("mobile", mobile);
		paramsMap.put("name", name);
		paramsMap.put("idcard", idcard);
		paramsMap.put("drivernum", drivernum);
		//TODO:调用ClientUserMapper接口方法，在客户信息表插入数据
		clientUserMapper.addUserInfo(paramsMap);
		//日志插入
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "在客户信息表插入数据");
		busMap.put("busitype", "插入");
		busMap.put("busiconent","在客户信息表插入数据");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		
		//TODO:把客户企业信息参数封装成map
		if(busilicense!=null && busilicense!=""){
			Map<String,String> paramsMap2=new HashMap<String,String>();
			paramsMap2.put("busilicense", busilicense);
			paramsMap2.put("cid", cid);
			paramsMap2.put("orgcode", orgcode);
			paramsMap2.put("taxcertno", taxcertno);
			//TODO:调用ClienCompanyrMapper接口方法，在客户企业表插入数据
			
			clientCompanyMapper.addClientCompany(paramsMap2);
		}
		
		//日志插入
		Map<String,Object> busMap2 = new HashMap<String,Object>();
		busMap2.put("logid", UUID.randomUUID().toString());
		busMap2.put("businame", "客户企业表插入数据");
		busMap2.put("busitype", "插入");
		busMap2.put("busiconent","客户企业表插入数据");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap2);
		
		//TODO:把订单信息封装到map
		Map<String,String> paramsMap3=new HashMap<String,String>();
		String orderid = UUID.randomUUID().toString();
		paramsMap3.put("orderid", orderid);
		//订单总额
		paramsMap3.put("shouldtotal", shouldtotal);
		paramsMap3.put("shouldrent", shouldrent);
		paramsMap3.put("carid", carid);
		paramsMap3.put("basicinsure", basicinsure);
		paramsMap3.put("btrentdept", btrentdept);
		paramsMap3.put("etrentdept", etrentdept);
		paramsMap3.put("readybttime", readybttime);
		paramsMap3.put("readettime", readettime);
		paramsMap3.put("clientid", uid);
		//生成订单时，将订单的状态默认为1，预订单
		paramsMap3.put("orderstate", "1");
		//TODO:调用CarOrderMapper接口方法，将数据插入到订单表中
		carOrderMapper.addCarOrder(paramsMap3);
		//日志插入
		Map<String,Object> busMap3 = new HashMap<String,Object>();
		busMap3.put("logid", UUID.randomUUID().toString());
		busMap3.put("businame", "客户企业表插入数据");
		busMap3.put("busitype", "插入");
		busMap3.put("busiconent","客户企业表插入数据");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap3);
		
		//TODO:将预定的车辆状态改为11，已租赁
		Map<String,String> paramsMap4=new HashMap<String,String>();
		paramsMap4.put("carid", carid);
		paramsMap4.put("carstate", "11");
		carMapper.updateCarState(paramsMap4);
		//日志插入
		Map<String,Object> busMap4 = new HashMap<String,Object>();
		busMap4.put("logid", UUID.randomUUID().toString());
		busMap4.put("businame", "根据汽车id修改状态");
		busMap4.put("busitype", "更新");
		busMap4.put("busiconent","根据汽车id修改状态");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap4);
	}
	
	/**
	 * 点击下一步时，查询所有门店信息getDeptInfo
	 */
	@RequestMapping(value="/getDeptInfo", method=RequestMethod.GET)
	public Map<String,Object> getCars(){
		List<Dept> deptList = deptMapper.getDepts();
		Map<String,Object> rsMap = new HashMap<String,Object>();
		rsMap.put("deptList", deptList);
		return rsMap;
	}
	
}
